<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="园区名" prop="parkName">
        <el-input v-model="dataForm.parkName" placeholder="园区名"></el-input>
      </el-form-item>
      <el-form-item label="园区简介" prop="parkSummary">
        <el-input v-model="dataForm.parkSummary" placeholder="园区简介"></el-input>
      </el-form-item>
      <el-form-item label="园区图片" prop="parkImage">
        <el-upload
          class="avatar-uploader"
          :action="url"
          :show-file-list="false"
          :on-success="handleParkSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img
            v-if="dataForm.parkImage"
            :src="dataForm.parkImage"
            class="avatar"
            style="width: 60%; height: auto; display: block"
          />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
        <!-- <el-input v-model="dataForm.parkImage" placeholder="园区图片"></el-input>-->
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      url: '',
      dataForm: {
        id: 0,
        parkName: '',
        parkSummary: '',
        parkImage: '',
      },
      dataRule: {
        parkName: [{ required: true, message: '园区名不能为空', trigger: 'blur' }],
        parkSummary: [{ required: true, message: '园区简介不能为空', trigger: 'blur' }],
        parkImage: [{ required: true, message: '园区图片不能为空', trigger: 'blur' }],
      },
    };
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.url = this.$http.adornUrl(`/sys/oss/uploadPark?token=${this.$cookie.get('token')}`);
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/basic/park/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.parkName = data.sysPark.parkName;
              this.dataForm.parkSummary = data.sysPark.parkSummary;
              this.dataForm.parkImage = data.sysPark.parkImage;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/basic/park/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              parkName: this.dataForm.parkName,
              parkSummary: this.dataForm.parkSummary,
              parkImage: this.dataForm.parkImage,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit('refreshDataList');
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    handleParkSuccess(res) {
      this.dataForm.parkImage = res.url;
    },
  },
};
</script>
